<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Menu.php
 * Created on : 2020-7-25 11:40:06
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use app\admin\model\AdminMenus;
use haoteam\service\MenuService;
use haoteam\service\DataExtend;
use haoteam\service\UtilService;
use haoteam\http\validates\AdminMenusValidate;

/**
 * 系统菜单管理
 * Class Menu
 * Tiger <1192851302@qq.com> 2020-7-25 11:40:06
 */
class Menus {

    /**
     * 系统菜单管理
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    菜单列表
     * @param Request $request
     * @return type
     */
    public function index(Request $request) {
        $data = AdminMenus::getAlls();
        foreach ($data as &$vo) {
            $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
        }
        return app('json')->successful($data);
    }

    /**
     * 后台菜单
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @desc    后台菜单
     * @param Request $request
     * @return type
     */
    public function get() {
        /* ! 读取当前权限菜单树 */
        return app('json')->successful(MenuService::instance()->getTree());
    }

    /**
     * 节点列表
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @desc    读取系统菜单功能节点
     * @return type
     */
    public function node_list() {
        return app('json')->successful(MenuService::instance()->getList());
    }

    /**
     * 上级菜单列表
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @return array    menus   列出可选上级菜单
     */
    public function parent_list(Request $request) {
        $menus = AdminMenus::getAlls(['status' => 1]);
        $_menus = DataExtend::arr2table(array_merge($menus, [['id' => 0, 'pid' => -1, 'jump' => '#', 'title' => '顶部菜单']]));
        foreach ($_menus as $key => $menu) {
            if ($menu['spt'] >= 3 || $menu['jump'] !== '#') {
                unset($_menus[$key]);
            }
        }
        return app('json')->successful($_menus);
    }

    /**
     * 新增菜单
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function create() {
        $post = UtilService::postMore([
                    'pid',
                    'name',
                    'title',
                    ['jump', '#'],
                    'node',
                    'query',
                    'icon'
        ]);
        $validate = new AdminMenusValidate();
        if ($validate->scene('create')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }

        try {
            $_data = AdminMenus::create($post)->toArray();
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('创建成功', $_data);
    }

    /**
     * 更新菜单
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function update() {
        $post = UtilService::postMore([
                    'id',
                    'pid',
                    'name',
                    'title',
                    ['jump', '#'],
                    'node',
                    ['query',''],
                    'icon'
        ]);

        $validate = new AdminMenusValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }

        try {
            AdminMenus::update($post, ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('编辑成功');
    }

    /**
     * 快速更新菜单
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc 此处只能更新指定字段
     * @return string msg 结果描述
     */
    public function save() {
        $post = UtilService::postMore([
                    ['ids', []],
                    'field',
                    'value'
        ]);
        $validate = new AdminMenusValidate();
        if ($validate->scene('save')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }

        try {
            AdminMenus::whereIn('id', $post['ids'])->update([$post['field'] => $post['value']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('修改成功');
    }

    /**
     * 删除菜单
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function delete() {
        $post = UtilService::postMore([
                    ['ids', []]
        ]);
        if (empty($post['ids'])) {
            return app('json')->fail('参数错误ID');
        }
        try {
            AdminMenus::whereIn('id', $post['ids'])->delete();
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('删除成功');
    }

}
